﻿// 修改密码

//根据url获取用户名
var parameter = window.location.href.split("?")[1];
var parameterList = parameter.split("&");
var username = decodeURIComponent(parameterList[0]);
var password;
var error = 0;

//根据用户名获取其用户信息
$.ajax({
    type: "post",
    url: "/ysfwpt/selectUserInfoByName",
    async: false,
    data: {
        username: username
    },
    success: function(data) {
        // console.log("success");
        // console.log(data);

        $("#job_number").val(data.user.job_number);
        $("#password").val(data.user.password);
        password = data.user.password;
    }
});

// 旧密码确认
function checkPSW(psw){
    if(psw == password){
        $("#psw_info1").html("密码输入正确");
        $("#psw_info1").removeClass("psw_info_error").addClass('psw_info_right');
    }
    else{
        $("#psw_info1").html("密码输入错误");
        $("#psw_info1").removeClass("psw_info_right").addClass('psw_info_error');
    }
}

// 新密码强度
function checkStrong(sPW){
    if(sPW.length<=3){
        error = 1;
        return 1; //弱
    }
    else if(sPW.length<=6){
        error = 0;
        return 2; //中
    }
    else if(sPW.length<=9){
        error = 0;
        return 3; //强
    }
}

function pwStrength(pwd){
    Dfault_color="#EEEEEE"; //灰色
    L_color="#FF0000"; //红色
    M_color="#FF9900"; //橙色
    H_color="#33CC00"; //绿色
    if (pwd==null || pwd==''){
        Lcolor=Mcolor=Hcolor=Dfault_color;
    }
    else{
        S_level=checkStrong(pwd);
        switch(S_level) {
            case 1:
                Lcolor=L_color;
                Mcolor=Hcolor=Dfault_color;
                break;
            case 2:
                Lcolor=Mcolor=M_color;
                Hcolor=Dfault_color;
                break;
            case 3:
                Lcolor=Mcolor=Hcolor=H_color;
        }
    }
    $("#strength_L").css("background",Lcolor);
    $("#strength_M").css("background",Mcolor);
    $("#strength_H").css("background",Hcolor);
}

//新密码确认
function checkPSW2(){
    var password_new = $("#password_new").val();
    var password_new2 = $("#password_new2").val();
    if(password_new2 == "" && password_new == ""){
        $("#psw_info2").html("请输入密码");
        $("#psw_info2").removeClass("psw_info_right").addClass('psw_info_error');
    }
    else if(password_new2 == password_new){
        $("#psw_info2").html("两次密码输入一致");
        $("#psw_info2").removeClass("psw_info_error").addClass('psw_info_right');
    }
    else{
        $("#psw_info2").html("两次密码输入不一致");
        $("#psw_info2").removeClass("psw_info_right").addClass('psw_info_error');
    }
}

//更新
$("#update").click(function (){
    var password_old = $("#password").val();
    var password_new = $("#password_new").val();
    var password_new2 = $("#password_new2").val();
    if(password_old == "" || password_new == "" || password_new2 == ""){
        alert("请输入密码");
    }
    else if(password_old != password){
        alert("旧密码错误");
    }
    else if(password_new != password_new2){
        alert("两次密码输入不一致");
    }
    else if(error != 0){
        alert("密码过于简单");
    }
    else {
        var formData = new FormData($("#updateform")[0]);
        $.ajax({
            type: "post",
            url: "/ysfwpt/updatePSW",
            data: formData,
            cache: false,
            contentType: false,
            processData: false,
            success: function (data) {
                // console.log("success");
                // alert("修改成功！");
                location.reload();
            },
            error: function (data) {

            }
        });
    }
});